package socket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 */
public class Client {
    private Socket socket;

    public Client(){
        try {
            System.out.println("等待连接..");
            socket = new Socket("localhost",6606);
            System.out.println("已连接");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try(PrintWriter pw = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream())),true)) {
            Scanner scanner = new Scanner(System.in);
            String s;
            while (true){
                System.out.println("请输入：");
                s = scanner.next();
                if ("exit".equals(s)){
                    break;
                }else {
                    pw.println(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
